export interface ApiResponse<T = unknown> {
  code: number;
  message: string;
  data?: T;
}

export class NextApiResponse {
  // 成功响应 (Next.js Response)
  static success<T>(data?: T, message = 'Success', status = 200) {
    return Response.json(ApiResponseUtil.create(status, message, data), {
      status,
    });
  }

  // 创建成功响应
  static created<T>(data?: T, message = 'Created successfully') {
    return Response.json(ApiResponseUtil.created(data, message), {
      status: 201,
    });
  }

  // 错误响应
  static error(message: string, status = 400) {
    return Response.json(ApiResponseUtil.create(status, message), { status });
  }

  // 常用错误响应快捷方法
  static badRequest(message = 'Bad request') {
    return Response.json(ApiResponseUtil.badRequest(message), { status: 400 });
  }

  static unauthorized(message = 'Unauthorized') {
    return Response.json(ApiResponseUtil.unauthorized(message), {
      status: 401,
    });
  }

  static forbidden(message = 'Forbidden') {
    return Response.json(ApiResponseUtil.forbidden(message), { status: 403 });
  }

  static notFound(message = 'Not found') {
    return Response.json(ApiResponseUtil.notFound(message), { status: 404 });
  }

  static validationError(message = 'Validation failed') {
    return Response.json(ApiResponseUtil.validationError(message), {
      status: 422,
    });
  }

  static serverError(message = 'Internal server error') {
    return Response.json(ApiResponseUtil.serverError(message), { status: 500 });
  }
}

/**
 * ApiResponse 工具类 - 提供便捷的静态方法
 */
export class ApiResponseUtil {
  // 成功响应
  static success<T>(data?: T, message = 'Success'): ApiResponse<T> {
    return {
      code: 200,
      message,
      data,
    };
  }

  // 创建成功响应
  static created<T>(
    data?: T,
    message = 'Created successfully'
  ): ApiResponse<T> {
    return {
      code: 201,
      message,
      data,
    };
  }

  // 客户端错误响应
  static badRequest(message = 'Bad request'): ApiResponse {
    return {
      code: 400,
      message,
    };
  }

  // 未授权响应
  static unauthorized(message = 'Unauthorized'): ApiResponse {
    return {
      code: 401,
      message,
    };
  }

  // 禁止访问响应
  static forbidden(message = 'Forbidden'): ApiResponse {
    return {
      code: 403,
      message,
    };
  }

  // 未找到响应
  static notFound(message = 'Not found'): ApiResponse {
    return {
      code: 404,
      message,
    };
  }

  // 请求冲突响应
  static conflict(message = 'Conflict'): ApiResponse {
    return {
      code: 409,
      message,
    };
  }

  // 参数验证失败响应
  static validationError(message = 'Validation failed'): ApiResponse {
    return {
      code: 422,
      message,
    };
  }

  // 服务器错误响应
  static serverError(message = 'Internal server error'): ApiResponse {
    return {
      code: 500,
      message,
    };
  }

  // 服务不可用响应
  static serviceUnavailable(message = 'Service unavailable'): ApiResponse {
    return {
      code: 503,
      message,
    };
  }

  // 通用响应创建方法
  static create<T>(code: number, message: string, data?: T): ApiResponse<T> {
    return {
      code,
      message,
      data,
    };
  }

  // 从 Response 对象转换 (用于 fetch 请求)
  static async fromResponse<T>(response: Response): Promise<ApiResponse<T>> {
    try {
      const data = await response.json();
      return {
        code: response.status,
        message: response.statusText || 'Response received',
        data,
      };
    } catch {
      return {
        code: response.status,
        message: response.statusText || 'Response received',
      };
    }
  }

  // 检查是否为成功响应
  static isSuccess(response: ApiResponse): boolean {
    return response.code >= 200 && response.code < 300;
  }

  // 检查是否为错误响应
  static isError(response: ApiResponse): boolean {
    return response.code >= 400;
  }

  // 检查是否为客户端错误
  static isClientError(response: ApiResponse): boolean {
    return response.code >= 400 && response.code < 500;
  }

  // 检查是否为服务器错误
  static isServerError(response: ApiResponse): boolean {
    return response.code >= 500;
  }
}
